package org.solrcn.hadoop.index;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.lucene.analysis.cjk.CJKAnalyzer;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.NoMergePolicy;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.NoLockFactory;
import org.apache.lucene.util.Version;
import org.solrcn.hadoop.util.HDFSUtil;
import org.solrcn.lucene.hdfs.HdfsDirectory;

public class PollLocal {

	final static Version matchVersion = Version.LUCENE_43;

	private static IndexWriter getFSWriter(final String outputDir)
			throws IOException {

		Directory directory = FSDirectory.open(new File(outputDir));
		directory.setLockFactory(NoLockFactory.getNoLockFactory());
		IndexWriterConfig iwc = new IndexWriterConfig(matchVersion,
				new CJKAnalyzer(matchVersion));
		iwc.setMergePolicy(NoMergePolicy.NO_COMPOUND_FILES);
		iwc.setRAMBufferSizeMB(512);
		return new IndexWriter(directory, iwc);
	}

	public static void main(String[] args) throws IOException {

		IndexWriter writer = null;

		FileSystem fs = HDFSUtil.getFileSystem("sc1", 9000);
		List<String> listDir = HDFSUtil.listDir(fs, "/");

		String indexPath = "/home/hanxuzhao/811";// 索引合并到本地的目录
		writer = getFSWriter(indexPath);

		List<HdfsDirectory> dirs = new ArrayList<HdfsDirectory>();

		String hdfsIndexDir = "index";// hdfs中索引存在的目录
		Configuration conf = new Configuration();
		for (int i = 0; i < listDir.size(); i++) {

			HdfsDirectory srcDirectory = new HdfsDirectory("hdfs://sc1:9000/user/hadoop/index",listDir.get(i));
			dirs.add(srcDirectory);

		}

		Directory[] dir = new Directory[dirs.size()];// Directory数组

		for (int i = 0; i < dir.length; i++) {
			dir[i] = dirs.get(i);
		}

		writer.addIndexes(dir);
		writer.forceMerge(1);
		writer.close();

	}
}
